﻿@{
    ViewData["Title"] = "Home Page";
}
<script src="/lib/jquery/jquery.min.js"></script>
<style>

</style>

<div>
    <div>
        <textarea name="content" id="content"></textarea>
        <button onclick="submit()">提交</button>
        <div id="tip"></div>
    </div>
    <div>
        <textarea name="content2" id="content2">{"a":"1","b":"2","c":"3"}</textarea>
        <button onclick="submit2()">提交2</button>
        <div id="tip2"></div>
    </div>
    <div>
        <textarea name="content3" id="content3">{"a":"1","b":"2","c":"3"}</textarea>
        <button onclick="submit3()">提交3 [FormBody]</button>
        <div id="tip3"></div>
    </div>
</div>

<script>
    function submit(){
        var value = $("#content").val();
        $.ajax({
            url:"/Home/Submit",
            method: "POST",
            data: value,
            complete: (a,b) => {
                console.log(a,b);
                $("#tip").text(a.responseText);
            }
        })
    }

    function submit2() {
        var value = $("#content2").val();
        $.ajax({
            url: "/Home/Submit2",
            method: "POST",
            data: value,
            complete: (a, b) => {
                console.log(a, b);
                $("#tip2").text(a.responseText);
            }
        })
    }

    // 提交 接口加了[FromBody]
    function submit3(){
        var value = $("#content3").val();
        try{
            JSON.parse(value);
        }
        catch(e)
        {
            $("#tip3").text(e);
            return;
        }
        $.ajax({
            url:"/Home/Submit3",
            method: "POST",
            contentType: "application/json",
            data: value,
            complete: (a, b) => {
                console.log(a,b);
                $("#tip3").text(a.responseText);
            }
        })
    }
</script>